package com.example.sixth.coree.utils;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MiscUtil {
    private static final long EXPIRE_TIME = 45 * 60 * 1000;//默认45分钟
    private static final String TOKEN_SECRET = " 1";
    public static String createToken(String userId) {
        try {
            // 设置过期时间  默认十五分钟
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            // 私钥和加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            // 设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("Type", "Jwt");
            header.put("alg", "HS256");
            // 返回token字符串
            return JWT.create()
                    .withHeader(header)
                    .withClaim("userId", userId)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    public static String createToken(String userId,Integer second) {
        try {
            // 设置过期时间  默认十五分钟
            Date date = new Date(System.currentTimeMillis() + second*1000);
            // 私钥和加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            // 设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("Type", "Jwt");
            header.put("alg", "HS256");
            // 返回token字符串
            return JWT.create()
                    .withHeader(header)
                    .withClaim("userId", userId)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    public static String verifyToken(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            String userId = jwt.getClaim("userId").asString();
            return userId;
        } catch (Exception e){
            return null;
        }
    }
    public static String getres(String key)  {
        key=key.replace(" ","&nbsp");
        String url = "http://bd.kuwo.cn/api/www/search/searchMusicBykeyWord?key="+key+"&pn=1&rn=30";
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);
        get.addHeader("Referer","http://bd.kuwo.cn");
        String randomString= RandomUtil.randomNumbers(8);
        get.addHeader("csrf",randomString);
        get.addHeader("Cookie","kw_token="+randomString);
        CloseableHttpResponse response = null;
        String s="";
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s.replace("&nbsp"," ");
    }
    public static String getLRC(String key)  {
        String url = "http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId="+key;
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);
        get.addHeader("Referer","http://bd.kuwo.cn");
        String randomString= RandomUtil.randomNumbers(8);
        get.addHeader("csrf",randomString);
        get.addHeader("Cookie","kw_token="+randomString);
        CloseableHttpResponse response = null;
        String s="";
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        s=s.replace("&nbsp"," ");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",JSON.parseObject(s).getJSONObject("data").getJSONArray("lrclist"));
        jsonObject.put("code",200);
        jsonObject.put("msg","获取成功");
        return jsonObject.toJSONString();
    }
}
